/*
 * Jasebt_appView.java
 */

package jasebt_app;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import xmlparser.*;
import base.*;
import java.awt.Color;


/**
 * The application's main frame.
 */
public class Jasebt_bugView extends FrameView {

    int bugNumber = 0;
    Bug b;
    boolean bugOpened = false;
    public static SAXParser parser;

    public static User currentUser;
    boolean logined = false;

    public static User user[];
    public String state[];
    public static String device[];
    public static Project project[];

    int usersCount = 0;
    public static String DBPATH = "DB" + File.separator;

    public static String VERSION = "0.5.1.3";

    public Jasebt_bugView(SingleFrameApplication app) throws SAXException, ParserConfigurationException, IOException {
        super(app);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        parser = factory.newSAXParser();
        b = new Bug();
        initComponents();

        jLabel1.setText("of " + (Tools.getLastBug() - 1));

        fillWithUsers();
        fillStates();
        fillProjects();
        fillDevices();
        validateUIforUser(currentUser);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Jasebt_appApp.getApplication().getMainFrame();
            aboutBox = new Jasebt_appAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Jasebt_appApp.getApplication().show(aboutBox);
    }

    @Action
    public void showNewBugView() throws SAXException, ParserConfigurationException {
        if (newBugForm == null) {
            JFrame mainFrame = Jasebt_appApp.getApplication().getMainFrame();
            newBugForm = new newBugView(mainFrame);
            newBugForm.setLocationRelativeTo(mainFrame);
        }
        Jasebt_appApp.getApplication().show(newBugForm);
    }

    @Action
    public void searchBugView() throws SAXException, ParserConfigurationException, IOException {
        if (searchBugForm == null) {
            JFrame mainFrame = Jasebt_appApp.getApplication().getMainFrame();
            searchBugForm = new searchBugView(mainFrame);
            searchBugForm.setLocationRelativeTo(mainFrame);
        }
        Jasebt_appApp.getApplication().show(searchBugForm);
    }

    @Action
    public void closedBugsView() throws SAXException, IOException, ParserConfigurationException {
        if (closedBugsForm == null) {
            JFrame mainFrame = Jasebt_appApp.getApplication().getMainFrame();
            closedBugsForm = new closedBugsView(mainFrame);
            closedBugsForm.setLocationRelativeTo(mainFrame);
        }
        Jasebt_appApp.getApplication().show(closedBugsForm);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        fFixVer = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        bOpen = new javax.swing.JButton();
        fTestVer = new javax.swing.JTextField();
        fBug = new javax.swing.JTextField();
        cbProject = new javax.swing.JComboBox();
        fTitle = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        bSubmit = new javax.swing.JButton();
        cbPriority = new javax.swing.JComboBox();
        cbAssignee = new javax.swing.JComboBox();
        bClose = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        fAuthor = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDescription = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        cbState = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        taComments = new javax.swing.JTextArea();
        bAddComment = new javax.swing.JButton();
        cbTester = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        fLogin = new javax.swing.JTextField();
        bLogin = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        cbDevice = new javax.swing.JComboBox();
        fPassword = new javax.swing.JPasswordField();
        bRight = new javax.swing.JButton();
        bLeft = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        miNewBug = new javax.swing.JMenuItem();
        miSearchBug = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        miClosedBugs = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                mainPanelFocusGained(evt);
            }
        });

        fFixVer.setEditable(false);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jasebt_app.Jasebt_appApp.class).getContext().getResourceMap(Jasebt_bugView.class);
        fFixVer.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fFixVer.setName("fFixVer"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        bOpen.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        bOpen.setText(resourceMap.getString("bOpen.text")); // NOI18N
        bOpen.setName("bOpen"); // NOI18N
        bOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOpenActionPerformed(evt);
            }
        });

        fTestVer.setEditable(false);
        fTestVer.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fTestVer.setName("fTestVer"); // NOI18N

        fBug.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fBug.setText(resourceMap.getString("tBugNumber.text")); // NOI18N
        fBug.setName("tBugNumber"); // NOI18N

        cbProject.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbProject.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Project 1", "Project 2", "Project 3" }));
        cbProject.setEnabled(false);
        cbProject.setName("cbProject"); // NOI18N

        fTitle.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fTitle.setName("fTitle"); // NOI18N

        jLabel14.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        bSubmit.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        bSubmit.setText(resourceMap.getString("submitButton.text")); // NOI18N
        bSubmit.setEnabled(false);
        bSubmit.setName("submitButton"); // NOI18N
        bSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSubmitActionPerformed(evt);
            }
        });

        cbPriority.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbPriority.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5" }));
        cbPriority.setEnabled(false);
        cbPriority.setName("cbPriority"); // NOI18N

        cbAssignee.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbAssignee.setEnabled(false);
        cbAssignee.setName("cbAssignee"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jasebt_app.Jasebt_appApp.class).getContext().getActionMap(Jasebt_bugView.class, this);
        bClose.setAction(actionMap.get("quit")); // NOI18N
        bClose.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        bClose.setText(resourceMap.getString("bClose.text")); // NOI18N
        bClose.setName("bClose"); // NOI18N
        bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCloseActionPerformed(evt);
            }
        });

        jLabel8.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        fAuthor.setEditable(false);
        fAuthor.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fAuthor.setName("authorField"); // NOI18N

        jLabel15.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        jLabel16.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        jLabel17.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel17.setText(resourceMap.getString("jLabel17.text")); // NOI18N
        jLabel17.setName("jLabel17"); // NOI18N

        jLabel19.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel19.setText(resourceMap.getString("jLabel19.text")); // NOI18N
        jLabel19.setName("jLabel19"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        taDescription.setColumns(20);
        taDescription.setEditable(false);
        taDescription.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        taDescription.setRows(5);
        taDescription.setName("taDescription"); // NOI18N
        jScrollPane1.setViewportView(taDescription);

        jLabel5.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        cbState.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbState.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Open", "Assigned", "Fixed", "Reopen", "Invalid", "Closed" }));
        cbState.setEnabled(false);
        cbState.setName("cbState"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        taComments.setColumns(20);
        taComments.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        taComments.setRows(5);
        taComments.setName("taComments"); // NOI18N
        jScrollPane2.setViewportView(taComments);

        bAddComment.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        bAddComment.setText(resourceMap.getString("bComment.text")); // NOI18N
        bAddComment.setEnabled(false);
        bAddComment.setName("bComment"); // NOI18N
        bAddComment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddCommentActionPerformed(evt);
            }
        });

        cbTester.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbTester.setEnabled(false);
        cbTester.setName("cbTester"); // NOI18N

        jLabel9.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        fLogin.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        fLogin.setText(resourceMap.getString("fLogin.text")); // NOI18N
        fLogin.setName("fLogin"); // NOI18N
        fLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fLoginMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                fLoginMouseEntered(evt);
            }
        });

        bLogin.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        bLogin.setText(resourceMap.getString("bLogin.text")); // NOI18N
        bLogin.setName("bLogin"); // NOI18N
        bLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginActionPerformed(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        cbDevice.setFont(resourceMap.getFont("taDescription.font")); // NOI18N
        cbDevice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbDevice.setEnabled(false);
        cbDevice.setName("cbDevice"); // NOI18N

        fPassword.setFont(resourceMap.getFont("fPassword.font")); // NOI18N
        fPassword.setText(resourceMap.getString("fPassword.text")); // NOI18N
        fPassword.setName("fPassword"); // NOI18N

        bRight.setText(resourceMap.getString("bRight.text")); // NOI18N
        bRight.setName("bRight"); // NOI18N
        bRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRightActionPerformed(evt);
            }
        });

        bLeft.setText(resourceMap.getString("bLeft.text")); // NOI18N
        bLeft.setName("bLeft"); // NOI18N
        bLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLeftActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                    .addComponent(bAddComment, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(264, 264, 264))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(371, 371, 371))
                    .addComponent(fTitle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fBug, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bRight)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(136, 136, 136)
                        .addComponent(bOpen)))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(bSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bClose, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(fLogin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                    .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                    .addComponent(cbProject, javax.swing.GroupLayout.Alignment.LEADING, 0, 75, Short.MAX_VALUE)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbAssignee, javax.swing.GroupLayout.Alignment.LEADING, 0, 75, Short.MAX_VALUE)
                                    .addComponent(fAuthor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                                .addGap(10, 10, 10)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbTester, javax.swing.GroupLayout.Alignment.LEADING, 0, 79, Short.MAX_VALUE)
                                    .addComponent(cbPriority, 0, 79, Short.MAX_VALUE)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                    .addComponent(fTestVer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                    .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(bLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                    .addComponent(cbDevice, javax.swing.GroupLayout.Alignment.LEADING, 0, 79, Short.MAX_VALUE)
                                    .addComponent(fPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(cbState, 0, 79, Short.MAX_VALUE)
                                .addGap(10, 10, 10)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(fFixVer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))))
                        .addContainerGap())))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bOpen))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bLogin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cbDevice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel15)
                            .addComponent(jLabel19))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbProject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbPriority, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(jLabel16))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fTestVer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbAssignee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cbTester, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(fBug, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(bRight)
                            .addComponent(bLeft))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel17))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fFixVer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bClose)
                        .addComponent(bSubmit))
                    .addComponent(bAddComment))
                .addGap(48, 48, 48))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        miNewBug.setAction(actionMap.get("showNewBugView")); // NOI18N
        miNewBug.setText(resourceMap.getString("miNewBug.text")); // NOI18N
        miNewBug.setName("miNewBug"); // NOI18N
        miNewBug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miNewBugActionPerformed(evt);
            }
        });
        jMenu1.add(miNewBug);

        miSearchBug.setAction(actionMap.get("searchBugView")); // NOI18N
        miSearchBug.setText(resourceMap.getString("miSearchBug.text")); // NOI18N
        miSearchBug.setName("miSearchBug"); // NOI18N
        miSearchBug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miSearchBugActionPerformed(evt);
            }
        });
        jMenu1.add(miSearchBug);

        menuBar.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        miClosedBugs.setAction(actionMap.get("closedBugsView")); // NOI18N
        miClosedBugs.setText(resourceMap.getString("miClosedBugs.text")); // NOI18N
        miClosedBugs.setName("miClosedBugs"); // NOI18N
        miClosedBugs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miClosedBugsActionPerformed(evt);
            }
        });
        jMenu2.add(miClosedBugs);

        menuBar.add(jMenu2);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
}//GEN-LAST:event_bCloseActionPerformed


    private void openBug(int bugNumber) {
            File bugfile = new File(Tools.getFilePath(bugNumber));
            //System.out.println("Opening bug# " + Integer.toString(bugNumber));
            bugParser bugp = new bugParser();

            if (bugfile.exists()) {
                try {
                    parser.parse(bugfile, bugp);
                    b = bugp.getResult(); //geting result of parsing
                    bugOpened = true;
                    fillBugForm(b);
                    validateUIforUser(currentUser);
                } catch (SAXException ex) {
                    fTitle.setText("Error during xml parsing. Please check bugs the DB");
                    Logger.getLogger(Jasebt_bugView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    fTitle.setText("Info about this bug doesn't exists, acces denied or currupted. Please check bugs the DB");
                    Logger.getLogger(Jasebt_bugView.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                b = null;
                fTitle.setText("Info about this bug doesn't exists, acces denied or currupted. Please check bugs the DB");
                System.err.println("File [" + bugfile + "] doesn't exists!");
            }
    }

    private void bOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOpenActionPerformed
        //TODO: search bugs contains same title or open bug number
        if (!fBug.getText().isEmpty()) {
            bugNumber = Integer.parseInt(fBug.getText());
            openBug(bugNumber);
        }
    }//GEN-LAST:event_bOpenActionPerformed

    private void miNewBugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miNewBugActionPerformed
    }//GEN-LAST:event_miNewBugActionPerformed

    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
    }//GEN-LAST:event_aboutMenuItemActionPerformed

    private void fLoginMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fLoginMouseEntered
    }//GEN-LAST:event_fLoginMouseEntered

private void fLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fLoginMouseClicked
        fLogin.setText("");
}//GEN-LAST:event_fLoginMouseClicked

    private User getUser(String login) {
        User res = null;

        for (int i = 0; i < user.length; i++) {
            if (login.equals(user[i].getLogin())) {
                res = user[i];
                break;
            }
        }

        return res;
    }

private void bLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginActionPerformed
    currentUser = getUser(fLogin.getText());

    if (currentUser != null && logined == false) {
        System.out.println("Logined as [" + currentUser.getLogin() + "]");

        logined = true;
        bLogin.setText("logout");
        fLogin.setEnabled(false);
        fPassword.setEnabled(false);
    } else {
        logined = false;
        bLogin.setText("login");
        currentUser = null;
        fLogin.setEnabled(true);
        fPassword.setEnabled(true);
    }

    validateUIforUser(currentUser);
}//GEN-LAST:event_bLoginActionPerformed

    // TODO: implement matrix access
    private void validateUIforUser(User user) {
        if (user == null) {
            fAuthor.setEditable(false);
            fTestVer.setEditable(false);
            fFixVer.setEditable(false);
            fTitle.setEditable(false);
            
            bAddComment.setEnabled(false);
            bSubmit.setEnabled(false);
            taComments.setEditable(false);
            taDescription.setEditable(false);

            miNewBug.setEnabled(false);

            cbPriority.setEnabled(false);
            cbProject.setEnabled(false);
            cbDevice.setEnabled(false);
            cbAssignee.setEnabled(false);
            cbTester.setEnabled(false);
            cbState.setEnabled(false);

        } else if (bugOpened == true) {
            switch (user.getPrivileges()) {
                // Admin
                case 0:
                    // Admin can change everything! :)
                    fAuthor.setEditable(true);
                    fTestVer.setEditable(true);
                    fFixVer.setEditable(true);
                    fTitle.setEditable(true);
                    bAddComment.setEnabled(true);
                    bSubmit.setEnabled(true);
                    taComments.setEditable(true);
                    taDescription.setEditable(true);

                    miNewBug.setEnabled(true);

                    cbPriority.setEnabled(true);
                    cbProject.setEnabled(true);
                    cbDevice.setEnabled(true);
                    cbAssignee.setEnabled(true);
                    cbTester.setEnabled(true);
                    cbState.setEnabled(true);
                    break;

                // Manager
                case 1:
                    cbAssignee.setEnabled(true);
                    cbPriority.setEnabled(true);
                    bAddComment.setEnabled(true);
                    bSubmit.setEnabled(true);
                    break;

                // TL
                case 2:
                    cbAssignee.setEnabled(true);
                    bSubmit.setEnabled(true);
                    bAddComment.setEnabled(true);
                    cbState.setEnabled(true);
                    break;

                // SW Engineer
                case 3:
                    fFixVer.setEditable(true);
                    bSubmit.setEnabled(true);
                    bAddComment.setEnabled(true);
                    cbState.setEnabled(true);
                    break;

                // QA Engineer
                case 4:
                    // Only QA engeneer and Admin can submit new bug
                    miNewBug.setEnabled(true);
                    cbPriority.setEnabled(true);
                    bSubmit.setEnabled(true);
                    bAddComment.setEnabled(true);
                    cbState.setEnabled(true);
                    break;
            }
        } else {
            switch (user.getPrivileges()) {
                case 0:
                    miNewBug.setEnabled(true);
                    break;
                case 4:
                    miNewBug.setEnabled(true);
                    break;
            }

        }
    }

    // TODO: needs implement comment adding.
    // Storing locally array of temporary comments and write it on "Submit"
    private void updateBug(Bug bug) throws IOException {
        bug.setAssignee(cbAssignee.getSelectedItem().toString());
        bug.setProject(cbProject.getSelectedItem().toString());
        bug.setDevice(cbDevice.getSelectedItem().toString());
        bug.setBugVer(fTestVer.getText());

        bug.setPriority(Integer.parseInt(cbPriority.getSelectedItem().toString()));
        bug.setState(cbState.getSelectedIndex());

        bug.setFixVer(fFixVer.getText());

        bug.increaseReopen();

        saveBugFile(bug);
    }

    private void saveBugFile(Bug bug) throws IOException {
        xmlWriter xmlw = new xmlWriter(Tools.getFilePath(bug.getID()));
        xmlw.createFile(bug);
    }

private void bSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSubmitActionPerformed
        try {
            updateBug(b);
        } catch (IOException ex) {
            Logger.getLogger(Jasebt_bugView.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_bSubmitActionPerformed

private void bAddCommentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddCommentActionPerformed
    addComment(currentUser.getLogin(), taComments.getText());
    //TODO: needs implement writing this comments to the bugs DB
}//GEN-LAST:event_bAddCommentActionPerformed

private void mainPanelFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_mainPanelFocusGained
}//GEN-LAST:event_mainPanelFocusGained

private void miSearchBugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSearchBugActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_miSearchBugActionPerformed

private void miClosedBugsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miClosedBugsActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_miClosedBugsActionPerformed

private void bLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLeftActionPerformed
    if (bugNumber > 0) bugNumber--;
    openBug(bugNumber);
    fBug.setText(Integer.toString(bugNumber));
}//GEN-LAST:event_bLeftActionPerformed

private void bRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRightActionPerformed
    if (bugNumber < Tools.getLastBug() - 1) bugNumber++;
    openBug(bugNumber);
    fBug.setText(Integer.toString(bugNumber));
}//GEN-LAST:event_bRightActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAddComment;
    private javax.swing.JButton bClose;
    private javax.swing.JButton bLeft;
    private javax.swing.JButton bLogin;
    private javax.swing.JButton bOpen;
    private javax.swing.JButton bRight;
    private javax.swing.JButton bSubmit;
    private javax.swing.JComboBox cbAssignee;
    private javax.swing.JComboBox cbDevice;
    private javax.swing.JComboBox cbPriority;
    private javax.swing.JComboBox cbProject;
    private javax.swing.JComboBox cbState;
    private javax.swing.JComboBox cbTester;
    private javax.swing.JTextField fAuthor;
    private javax.swing.JTextField fBug;
    private javax.swing.JTextField fFixVer;
    private javax.swing.JTextField fLogin;
    private javax.swing.JPasswordField fPassword;
    private javax.swing.JTextField fTestVer;
    private javax.swing.JTextField fTitle;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem miClosedBugs;
    private javax.swing.JMenuItem miNewBug;
    private javax.swing.JMenuItem miSearchBug;
    private javax.swing.JTextArea taComments;
    private javax.swing.JTextArea taDescription;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
    private JDialog newBugForm;
    private JDialog searchBugForm;
    private JDialog closedBugsForm;

    private void fillBugForm(Bug b) {
        if (b != null) {
            fTitle.setText(b.getTitle());
            fAuthor.setText(b.getCreator());
            fTestVer.setText(b.getBugVer());
            fFixVer.setText(b.getFixVer());

            cbProject.setSelectedItem(b.getProject());
            cbPriority.setSelectedIndex(b.getPriority());
            cbState.setSelectedIndex(b.getState());
            cbAssignee.setSelectedItem(b.getAssignee());
            taDescription.setText("");

            taDescription.append(b.getDescription());
            taDescription.append("\n--------\n");

            for (int i = 0; i < b.getCommentsCount(); i++ ) {
                String author = b.getComment(i).getAuthor();
                String text = b.getComment(i).getText();
                addComment(author, text);
            }
        }
    }

    private void addComment(String author, String comment) {
        taDescription.append(author + ":\n");
        taDescription.append(comment);
        taDescription.append("\n---\n");
    }

    // returning users count
    private int OpenUsersDB() throws IOException, SAXException {
        int count = 0;
        userParser up = new userParser();
        File usersFile = new File(DBPATH + "users.xml");
        
        if (usersFile.exists() == true) {
            parser.parse(usersFile, up);
            count = up.getCount();
            user = new User[count];
            up.fill(user);
            System.out.println("Users count = " + count);
        }

        return count;
    }

    private int OpenStatesDB() throws SAXException, IOException {
        int count = 0;
        stateParser sp = new stateParser();
        File statesFile = new File(DBPATH + "states.xml");
        
        if (statesFile.exists() == true) {
            parser.parse(statesFile, sp);
            count = sp.getCount();
            state = new String[count];
            sp.fill(state);
        }

        return count;
    }
    private int OpenDevicesDB() throws SAXException, IOException {
        int count = 0;
        deviceParser dp = new deviceParser();
        File deviceFile = new File(DBPATH + "devices.xml");

        if (deviceFile.exists() == true) {
            parser.parse(deviceFile, dp);
            count = dp.getCount();
            device = new String[count];
            dp.fill(device);
        }

        return count;
    }

    private int OpenProjectsDB() throws SAXException, IOException {
        int count = 0;
        projectParser sp = new projectParser();
        File projectsFile = new File(DBPATH + "projects.xml");

        if (projectsFile.exists()) {
            parser.parse(projectsFile, sp);
            count = sp.getCount();
            System.out.println("count projects = " + count);
            project = new Project[count];
            sp.fill(project);
        }

        return count;
    }

    private void fillWithUsers() throws SAXException, IOException {
        int c = OpenUsersDB();

        taDescription.removeAll();
        cbAssignee.removeAllItems();
        cbTester.removeAllItems();

        for (int i = 0; i < c; i++) {
           cbAssignee.addItem(user[i].getLogin());
           cbTester.addItem(user[i].getLogin());
        }
    }

    private void fillStates() throws SAXException, IOException {
        int c = OpenStatesDB();
        cbState.removeAllItems();

        for (int i = 0; i < c; i++) {
            cbState.addItem(state[i]);
        }
    }

    private void fillDevices() throws SAXException, IOException {
        int c = OpenDevicesDB();
        cbDevice.removeAllItems();

        for (int i = 0; i < c; i++) {
            cbDevice.addItem(device[i]);
        }
    }
     private void fillProjects() throws SAXException, IOException {
        int c = OpenProjectsDB();
        cbProject.removeAllItems();

        for (int i = 0; i < c; i++) {
            cbProject.addItem(project[i].getName());
        }
    }
}
